<?
	include("../includes/main_inc.php");
	include("../../functions.inc.php");
	
	if($_GET['SFrom']) $SFrom=$_GET['SFrom'];
	elseif ($_POST['SFrom']) $SFrom=$_POST['SFrom'];
	if($_GET['SText']) $SText=$_GET['SText'];
	elseif ($_POST['STo']) $STo=$_POST['STo'];
	if($_GET['STo']) $STo=$_GET['STo'];
	elseif ($_POST['SText']) $SText=$_POST['SText'];
	if($_GET['SType']) $SType=$_GET['SType'];
	elseif ($_POST['SType']) $SType=$_POST['SType'];
	if($_GET['RType']) $RType=$_GET['RType'];
	elseif ($_POST['RType']) $RType=$_POST['RType'];
	else $RType="All";
	if($_GET['OType']) $OType=$_GET['OType'];
	elseif ($_POST['OType']) $OType=$_POST['OType'];
	else $OType="contactName";
	if($_GET['Page']) $Page=$_GET['Page'];
	elseif($_POST['Page']) $Page=$_POST['Page'];
	else $Page=1;
	
	
	
	$RecordsPerPage=10;
	
	if(!empty($SText)){
		$SearchSQL="AND $SType LIKE '%$SText%'";
	};
	if(!empty($SFrom)&&!empty($STo)){
		$SearchSQL.=" AND (PayDate>='".SwapDates($SFrom)."' AND PayDate<='".SwapDates($STo)." 23:59:59') ";
	}
	
	
	$SQL1="SELECT COUNT(*) FROM affiliate,Payments WHERE affiliate.id=Payments.affiliateID  $SearchSQL";
	$rset=$r->rawQuery($SQL1);
	$rdata=mysql_fetch_array($rset);
	$rcount=$rdata[0];
	$MaxPages=ceil($rcount/$RecordsPerPage);
	if($Page>$MaxPages) $Page=$MaxPages;
	$StartRecord=($Page-1)*$RecordsPerPage;
	if($StartRecord<0) $StartRecord=0;
	/*
	$SQL="SELECT DATE_FORMAT(bookingDate,'%a %e %b %Y'),DATE_FORMAT(checkin,'%a %e %b %Y'),domain,propertyName,affiliateCommission FROM bookings,affiliateDomains WHERE bookings.affiliateDomainsID=affiliateDomains.id AND  bookings.affiliateID='$_SESSION[AffKey]' $BSQL";
	*/
	$SQL2="SELECT DATE_FORMAT(PayDate,'%a %e %b %Y'),contactName,Amount FROM affiliate,Payments WHERE affiliate.id=Payments.affiliateID  $SearchSQL  ORDER BY $OType LIMIT $StartRecord,$RecordsPerPage";
	$rset=$r->rawQuery($SQL2);
	if(!$rset) print mysql_error();
	//print $SQL2;
	$TargetUrl="payment-status.php";
	$NPPage="RType=$RType&SFrom=$SFrom&STo=$STo&SType=$SType&OType=$OType&SText=".urlencode($SText);
	$RecTo=($StartRecord+$RecordsPerPage);
	if($RecTo>$rcount) $RecTo=$rcount;

?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Affiliate Bubble Administration</title>

<link href="<?=$rootdir?>css/admin.css" rel="stylesheet" type="text/css">
<script src="<?=$rootdir?>jscript/menuArray.js" language="javascript"></script>
<script src="<?=$rootdir?>jscript/clock.js" language="javascript"></script>
<script language="JavaScript" src="<?=$rootdir?>jscript/calendar1.js"></script><!-- Date only with year scrolling -->
</head>

<body onLoad="funClock();">
<?php include("../includes/header.php"); ?>
<table width="100%" border="0" cellspacing="25" cellpadding="0">
<tr>
<td valign="top" width="240"><div id="menu">
  <?php include("../menu/reportingmenu.php"); ?>
</div></td>
<td align="center" valign="top">

<div id="title">
Payment Status Report
</div>

<form name="form1" method="post" action="payment-status.php">
  <div id="search"> Enter search criteria.<br>
      <br>
      <table width="271" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="29">From:</td>
          <td width="102"><input name="SFrom" type="text" class="formFields" id="SFrom" value="<?=($_POST['SFrom'] ? $_POST['SFrom'] :date("d-m-Y"));?>" size="10">
              <strong><a href="javascript:cal1.popup();"><img src="<?=$rootdir?>jscript/img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a></strong></td>
          <td width="23">To:</td>
          <td width="117"><input name="STo" type="text" class="formFields" id="STo" value="<?=($_POST['STo'] ? $_POST['STo'] :date("d-m-Y"));?>" size="10">
              <strong><a href="javascript:cal2.popup();"><img src="<?=$rootdir?>jscript/img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date" /></a></strong></td>
        </tr>
      </table>
      <br>
      <table border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="110">Keywords</td>
          <td><input name="SText" type="text" class="formFields" id="SText" value="<?=$SText;?>" size="30"></td>
          <td width="66" align="center">Search By </td>
          <td><select name="SType" class="formFields" id="SType">
              <option value="contactName" <?=($SType=="contactName" ? "selected" : "");?>>Affiliate Name</option>
              
            </select>
          </td>
          <td width="50" align="center">Sort By </td>
          <td><select name="OType" class="formFields" id="OType">
              <option value="contactName">Affiliate Name</option>
			  <option value="Amount">Payment Amount</option>
          </select></td>
        </tr>
      </table>
    <p align="right">
        <input type="submit" name="Submit" value="Search" class="formButtons">
    </p>
    <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#E9EEF5">
      <tr id="formHeader">
        <td width="17%" align="center"><strong>Date</strong></td>
        <td width="66%" align="left"><strong>Affiliate Name  </strong></td>
        <td width="17%" align="center"><strong>Amount</strong></td>
        </tr>
      <?php
$Count=0;
if($rcount>0){
	while ($myrow = mysql_fetch_row($rset)) {
		$Total+=$myrow[2];
?>
      <tr bgcolor="<?=(($Count%2)==0 ? "#FFFFFF" : "#EBEBEB"); ?>">
        <td align="center" bgcolor="#FFFFFF"><?=$myrow[0];?></td>
        <td align="left" bgcolor="#FFFFFF"><?=$myrow[1];?></td>
        <td align="center" bgcolor="#FFFFFF">$
            <?=$myrow[2];?></td>
        </tr>
      <?
		};
	};
?>
      <tr>
        <td bgcolor="#FFFFFF">&nbsp;</td>
        <td align="right" bgcolor="#FFFFFF"><strong>TOTAL</strong></td>
        <td align="center" bgcolor="#FFFFFF"><strong>$
            <?=number_format($Total,2);?>
        </strong></td>
        </tr>
    </table>
    
  </div>
</form>
<div id="results">
  <p>
    <?=($StartRecord==0 ? ($RecTo==0 ? 0 : 1) : ($StartRecord+1));?>
    &nbsp;-&nbsp;
    <?=$RecTo;?>
    &nbsp;of&nbsp;
    <?=$rcount;?>
    &nbsp;Results</p>
  <p>
    <? if($Page>1){ ?>
    <a href="<?=$TargetUrl;?>?Page=<?=$Page-1;?>&<?=$NPPage?>" >&lt;&lt;Back</a>
    <? }; ?>
    <?
		for($x=1;$x<=$MaxPages;$x++){
			?>
    <a href="<?=$TargetUrl;?>?Page=<?=$x;?>&<?=$NPPage?>" >
    <?=$x?>
    </a>
    <?
		};
	  ?>
    <?
						if($Page<$MaxPages){
					?>
    <a href="<?=$TargetUrl;?>?Page=<?=$Page+1;?>&<?=$NPPage?>" >Next &gt;&gt; </a>
    <?
						};
					?>
  </p>
  <p><a href="payment-csv.php?<?=$NPPage;?>">EXPORT TO .CSV</a> </p>
</div>

</td>
</tr>
</table>
<script language="JavaScript">
<!-- // create calendar object(s) just after form tag closed
	 // specify form element as the only parameter (document.forms['formname'].elements['inputname']);
	 // note: you can have as many calendar objects as you need for your application
	var cal1 = new calendar1(document.forms['form1'].elements['SFrom']);
	cal1.year_scroll = true;
	cal1.time_comp = false;
	var cal2 = new calendar1(document.forms['form1'].elements['STo']);
	cal2.year_scroll = true;
	cal2.time_comp = false;
	
	

//-->
</script>
</body>
</html>
